﻿using Microsoft.EntityFrameworkCore;
using YDT_ProductService.Domains.Entities;
using YDT_ProductService.Domains.Enums;
using YDT_ProductService.Domains.Interfaces;
using YDT_ProductService.Infrastructure.Datas;

namespace YDT_ProductService.Infrastructure.Repositorys
{
    /// <summary>
    /// 商品仓储实现 - 实现商品数据访问
    /// </summary>
    public class ProductRepository : IProductRepository
    {
        private readonly ProductServiceDbContext _context;

        /// <summary>
        /// 构造函数
        /// </summary>
        public ProductRepository(ProductServiceDbContext context)
        {
            _context = context;
        }

        /// <summary>
        /// 根据ID获取商品
        /// </summary>
        public async Task<Product?> GetByIdAsync(Guid id)
        {
            return await _context.Products
                .Include(p => p.ProductImages) // 包含商品图片
                .FirstOrDefaultAsync(p => p.Id == id);
        }

        /// <summary>
        /// 根据商品编码获取商品
        /// </summary>
        public async Task<Product?> GetByProductCodeAsync(string productCode)
        {
            return await _context.Products
                .Include(p => p.ProductImages) // 包含商品图片
                .FirstOrDefaultAsync(p => p.ProductCode == productCode);
        }

        /// <summary>
        /// 获取所有商品
        /// </summary>
        public async Task<IEnumerable<Product>> GetAllAsync()
        {
            return await _context.Products
                .Include(p => p.ProductImages) // 包含商品图片
                .Where(p => p.ProductStatus != ProductStatus.Deleted) // 排除已删除商品
                .AsNoTracking() // 只读查询，不跟踪变更
                .ToListAsync();
        }

        /// <summary>
        /// 根据状态获取商品
        /// </summary>
        public async Task<IEnumerable<Product>> GetByStatusAsync(ProductStatus status)
        {
            return await _context.Products
                .Include(p => p.ProductImages) // 包含商品图片
                .Where(p => p.ProductStatus == status)
                .AsNoTracking() // 只读查询，不跟踪变更
                .ToListAsync();
        }

        /// <summary>
        /// 分页获取商品
        /// </summary>
        public async Task<(IEnumerable<Product> Products, int TotalCount)> GetPagedAsync(int pageIndex, int pageSize)
        {
            var query = _context.Products
                .Include(p => p.ProductImages) // 包含商品图片
                .Where(p => p.ProductStatus != ProductStatus.Deleted) // 排除已删除商品
                .OrderBy(p => p.ProductSort) // 按排序字段排序
                .ThenByDescending(p => p.CreateTime); // 然后按创建时间倒序

            var totalCount = await query.CountAsync();
            var products = await query
                .Skip((pageIndex - 1) * pageSize)
                .Take(pageSize)
                .AsNoTracking() // 只读查询，不跟踪变更
                .ToListAsync();

            return (products, totalCount);
        }

        /// <summary>
        /// 添加商品
        /// </summary>
        public async Task AddAsync(Product product)
        {
            await _context.Products.AddAsync(product);
            await _context.SaveChangesAsync();
        }

        /// <summary>
        /// 更新商品
        /// </summary>
        public async Task UpdateAsync(Product product)
        {
            _context.Products.Update(product);
            await _context.SaveChangesAsync();
        }

        /// <summary>
        /// 删除商品
        /// </summary>
        public async Task DeleteAsync(Guid id)
        {
            var product = await _context.Products.FindAsync(id);
            if (product != null)
            {
                _context.Products.Remove(product);
                await _context.SaveChangesAsync();
            }
        }

        /// <summary>
        /// 检查商品是否存在
        /// </summary>
        public async Task<bool> ExistsAsync(Guid id)
        {
            return await _context.Products.AnyAsync(p => p.Id == id);
        }

        /// <summary>
        /// 检查商品编码是否存在
        /// </summary>
        public async Task<bool> ProductCodeExistsAsync(string productCode)
        {
            return await _context.Products.AnyAsync(p => p.ProductCode == productCode);
        }
    }
}
